#include "setup.h"
#include <Arduino.h>
/***********************************
 * @brief   点灯APP
 */
#define BLINKER_WIFI              // wifi设备
#define BLINKER_MIOT_MULTI_OUTLET // 小爱 插座 设备,用于小爱音箱绑定识别设备类型,这个不加,同步不出来设备
#include <Blinker.h>              //blinker库

char auth[] = "492eff291676";
char ssid[] = "FAST_AC4E";
char pswd[] = "15070380093zjw";

bool oState[5] = {false};
void miotPowerState(const String &state, uint8_t num);
void miotQuery(int32_t queryCode, uint8_t num);
void dataRead(const String &data);
// ##################################
void serialRead();
// ##################################

void setup()
{
      Serial.begin(112500);

      keyboard_init();
      Ds1302_init();
      state_led_init();
      rgb_init();

      pinMode(BUZZ, OUTPUT);
      digitalWrite(BUZZ, HIGH);
      Blinker.begin(auth, ssid, pswd);
      Blinker.attachData(dataRead);
      BlinkerMIOT.attachPowerState(miotPowerState);
      BlinkerMIOT.attachQuery(miotQuery);
}
bool main_cpu_sleep = 0;
bool dial_or_lvgl = 0;
bool state_led_return = 0;

uint32_t state_led_time = 0;
void loop()
{
      // Blinker.run();
      keyboard_run();

      serialRead();

      rgb_run();
      if (millis() - state_led_time > 500)
      {
            state_led_sendData(LEEDDD);

            if (state_led_return)
            {
                  LEEDDD >>= 1;
                  if (LEEDDD == 0B00000011)
                        state_led_return = 0;
            }
            else
            {
                  LEEDDD <<= 1;
                  if (LEEDDD == 0B11000000)
                        state_led_return = 1;
            }
            state_led_time = millis();
      }
      state_led_sendData(LEEDDD);
}

/*******************
 * 串口收发函数
 *
 *******************/

void serialRead()
{
      if (Serial.available() > 0)
      {
            String uart_read = "";
            uart_read = char(Serial.read());
            uart_read += char(Serial.read());
            uart_read += char(Serial.read());
            uart_read += char(Serial.read());
            if (uart_read.startsWith("GTTM")) // 判断开头类型为获取时间
            {
                  DateTime now;
                  getDateTime(&now);
                  // Serial.printf("TM20%02d%02d%02d%02d%02d%02d%d",
                  //               now.year, now.month, now.day,
                  //               now.hour, now.minute, now.second,
                  //               now.dow);
                  Serial.printf("TM20%02d%02d%02dFULL", now.hour, now.minute, now.second);
            }
            else if (uart_read.startsWith("ZWAA"))
            {
                  if (char(Serial.read()) == '1')
                  {
                        LEEDDD = LEEDDD | 0b00000010;
                  }
                  else
                  {
                        LEEDDD = ~LEEDDD | 0b00000010;
                        LEEDDD = ~LEEDDD;
                  }
            }
      }
      while (Serial.available() > 0)
      {
            Serial.flush();
      }
}

/****************************
 * @brief   点灯APP  函数
 *
 *
 *
 */
void miotPowerState(const String &state, uint8_t num)
{
      // BLINKER_LOG("设置插座插孔: ", num, ", 电源状态为: ", state);

      if (state == BLINKER_CMD_ON)
      {
            digitalWrite(LED_BUILTIN, HIGH);

            BlinkerMIOT.powerState("on", num);
            BlinkerMIOT.print();

            Serial.print("XN");
            Serial.print(num);
      }
      else if (state == BLINKER_CMD_OFF)
      {
            digitalWrite(LED_BUILTIN, LOW);

            BlinkerMIOT.powerState("off", num);
            BlinkerMIOT.print();

            Serial.print("XF");
            Serial.print(num);
      }
}

void miotQuery(int32_t queryCode, uint8_t num)
{
      // BLINKER_LOG("MIOT Query outlet: ", num, ", codes: ", queryCode);
      switch (queryCode)
      {
      case BLINKER_CMD_QUERY_ALL_NUMBER:
            // BLINKER_LOG("MIOT Query All");
            BlinkerMIOT.powerState(oState[num] ? "on" : "off", num);
            BlinkerMIOT.print();
            break;
      case BLINKER_CMD_QUERY_POWERSTATE_NUMBER:
            // BLINKER_LOG("MIOT Query Power State");
            BlinkerMIOT.powerState(oState[num] ? "on" : "off", num);
            BlinkerMIOT.print();
            break;
      default:
            BlinkerMIOT.powerState(oState[num] ? "on" : "off", num);
            BlinkerMIOT.print();
            break;
      }
}

void dataRead(const String &data)
{
      // BLINKER_LOG("Blinker readString: ", data);

      Blinker.vibrate();

      uint32_t BlinkerTime = millis();

      Blinker.print("millis", BlinkerTime);
}
// #######################################################
